-- UIBuildingView2
-- Created by chengb Sep/17/2015
-- 建筑查看界面（含两个按钮）

UIBuildingView2 = class("UIBuildingView2", function()
    return cc.Layer:create();
end);

function UIBuildingView2.create(gridData, okFunc, cancelFunc, okText, cancelText, dialog, iconPath, hideBack, title)
    return UIBuildingView2.new(gridData, okFunc, cancelFunc, okText, cancelText, dialog, iconPath, hideBack, title);
end

function UIBuildingView2:ctor(gridData, okFunc, cancelFunc, okText, cancelText, dialog, iconPath, hideBack, title)
    -- 初始化
    self:setName("UIBuildingView2");
    local node = cc.CSLoader:createNode("layout/dungeon/BuildingView2.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    assert(type(gridData) == "table", "必须传入格子数据");

    self.gridData  = gridData;

    -- 查询建筑配置
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class) or {};

    local cfgOffsetY = buildingInfo["icon_offset_y"];
    if type(cfgOffsetY) ~= "number" then
        cfgOffsetY = 0;
    end

    self.title         = title or buildingInfo["title"];                               -- 标题
    self.iconPath      = iconPath or getLevelItemIconPath(buildingInfo["icon"]);       -- 图标路径
    self.dialog        = dialog or BuildingBonusM.getBuildingDialog(self.gridData);    -- 对话内容
    self.okBtnText     = okText or buildingInfo["view_text"];                          -- 确定按钮文本
    self.cancelBtnText = cancelText or buildingInfo["view_text2"];                     -- 取消按钮文本
    self.iconOffsetY   = cfgOffsetY;                                                   -- 图标纵向偏移量
    self.okFunc        = okFunc;                                                       -- 点击确定的回调函数
    self.cancelFunc    = cancelFunc;                                                   -- 点击取消的回调函数
    self.hideBack      = hideBack;                                                     -- 隐藏返回按钮

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIBuildingView2:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIBuildingView2:registerEventCallback()
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIBuildingView2 界面析构清理");
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end

-- 注册点击事件
function UIBuildingView2:registerTouchEvent()
    -- 注册返回按钮点击事件
    local node = self.node;
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIBuildingView2");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    if self.hideBack or self.cancelBtnText == getLocStr("btn_text_leave") then
        btnBack:setVisible(false);
    end

    -- 注册确定按钮点击事件
    local btnOk = findChildByName(node, "CT/btn_ok");
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");
            if type(self.okFunc) == 'function' then
                self.okFunc();
            end

            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIBuildingView2");
        end
    end
    AddTouchEventListener(btnOk, onOkClick);

    -- 注册取消按钮点击事件
    local btnCancel = findChildByName(node, "CT/btn_cancel");
    local function onCancelClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");
            if type(self.cancelFunc) == 'function' then
                self.cancelFunc();
            end

            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIBuildingView2");
        end
    end
    AddTouchEventListener(btnCancel, onCancelClick);
end

-- 重绘
function UIBuildingView2:redraw()
    local descLabel  = findChildByName(self.node, "CT/desc");
    local iconImg    = findChildByName(self.node, "CT/icon");
    local titleLabel = findChildByName(self.node, "CT/title_node/title");
    local btnOk      = findChildByName(self.node, "CT/btn_ok");
    local btnCancel  = findChildByName(self.node, "CT/btn_cancel");
    local kerning, kerning2 = 0, 0;
    if getLang() ~= "zh" and getLang() ~= "tw" then
        kerning, kerning2 = -3, -1;
    end

    titleLabel:setString(self.title);
    iconImg:loadTexture(self.iconPath);
    btnOk:setTitleText(self.okBtnText);
    btnCancel:setTitleText(self.cancelBtnText);

    iconImg:setPositionY(iconImg:getPositionY() + self.iconOffsetY);
    local iconSize = iconImg:getContentSize();

    iconImg:setScale(1.3);
    TextStyleM.setTextStyle(btnOk, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    TextStyleM.setTextStyle(btnCancel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    TextStyleM.setArtWordTitleStyle(titleLabel);
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, false, kerning2);

    applyString(descLabel, self.dialog);

    -- 确认按钮如果为"离开"，则返回按钮不显示
    if self.okBtnText == getLocStr("btn_text_leave") then
        local btnBack = findChildByName(self.node, "BT/btn_back");
		btnBack:setVisible(false);
    end
end










